<#-- 
 
 crea una extension para la clase para c# para la tabla  ${table}...
 basicamente para su uso como objectDataSource ...
 Author : Luis Molina...
 
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="${table}bll">
<#assign extensionFile="cs">
<#assign languageGenerated="c#">
<#assign description="c#">
<#assign targetDirectory="">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Data;
using System.Web;
using System.ComponentModel;


[System.ComponentModel.DataObject]
public partial class ${table}Bll
{
 
  [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)] 
  public static List< ${table} > Get${table}()
    {
         List< ${table} > lista${table}= new  List< ${table} >();
        dbClass db=new dbClass(ctes.conStringAdoGeneral);
        
        try
        {
            StringBuilder sb= new StringBuilder();
           
            sb.Append(" select ");
            sb.Append("${table.getListWithAllFields()}");
            sb.Append(" from ${table}");
            System.Data.DataSet ds = db.sqlDataset(sb.ToString());

             foreach (DataRow row in ds.Tables[0].Rows)
            {
                ${table} new${table}= new ${table}();
                <#assign count=0>		
				<#list table.getFields() as field> 
				 <#switch field.getType()>
						  <#case "_int">
							 new${table}.${field} = sf.entero(row["${field}"]);
							 <#break>
						  <#case "_string">
							 new${table}.${field} = sf.cadena(row["${field}"]);
							 <#break>
						  <#case "_date">
							 new${table}.${field} = sf.fecha(row["${field}"].ToString());
							 <#break>
						  <<#case "_boolean">
							 new${table}.${field} = sf.Bool(row["${field}"]);
							 <#break>
						  <#default>
							 new${table}.${field} = sf.cadena(row["${field}"]);
				</#switch>  
			 <#assign count = count+1>
			</#list>	

                lista${table}.Add(new${table} );
                new${table} =null;
            }
        }
        catch (System.Exception ex)
        {}
        finally
        {
         db.Dispose();
        } 

        //authors.Sort(New AuthorComparer(sortExpression))
        return lista${table};
   }

 
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
 public static string InsertBll(${table} obj${table})
    {            
        System.Text.StringBuilder sqlt = new System.Text.StringBuilder();
        string retorno = "";
        dbClass db = new dbClass(ctes.conStringAdoGeneral);
        SqlDataReader reg;
        try
        {
        sqlt.Append( " insert into ${table} (${table.getListWithAllFields()})"); 
        sqlt.Append( " ) values (");
			<#assign count=0>	
			<#list table.getFields() as field> 
				<#if (!field.getisKey())>
					 <#-- segun el tipo de campo...   -->
					 <#switch field.getType()>
							  <#case "_int">
			 					 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
								 <#break>
							  <#case "_string">
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
								 <#break>
							  <#case "_date">
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>")  ;
								 <#break>
							  <<#case "_boolean">
								 sqlt.Append( sf.mySql(obj${table}.${field})  + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>");
								 <#break>
							  <#default>
								 sqlt.Append( sf.mySql(obj${table}.${field}) + " <#if (table.getNumberOfFields() -  count  != 1)>,</#if>") ;
					</#switch>  
				</#if>
			 <#assign count = count+1>
			</#list>	 
			sqlt.Append(" )");

            db.ejecutar(sqlt.ToString());
            reg = db.sql("select @@identity as id from ${table}");
            if (reg.Read())
                {
                  retorno = sf.cadena(reg["id"].ToString());
                }
                reg.Close();
            }
                 
            catch (Exception ex)
            {
                // Argument is optional, no "When" keyword 
            }
            finally
            {
                db.Dispose();
              
            }
            return retorno;
       }

	
	public static void Delete(${table} obj${table})
    {
        string sqlt;
        dbClass db = new dbClass(ctes.conStringAdoGeneral);
        try { 
        sqlt = " delete from ${table} ";
        sqlt += " where 1!=0";  
        
        <#assign count=0>	
        <#list table.getFields() as field>
            <#if (field.getisKey())>
                <#if (field.getType()=="string")>
                    sqlt += " and ${field}='" + sf.cadena(obj${table}.${field})';
                <#else>
                     sqlt += " and ${field}=" + sf.cadena(obj${table}.${field});
                </#if>
            </#if>
         <#assign count = count+1>
		</#list>
           db.ejecutar(sqlt);

           }
           catch (Exception ex)
           {
               // Argument is optional, no "When" keyword 
           }
           finally
           {
               db.Dispose();

           }
    }
	
	
	[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
	public static bool UpdateBll(${table} obj${table})
		{
            SqlDataReader reg;
            System.Text.StringBuilder sqlt = new System.Text.StringBuilder();
            dbClass db = new dbClass(ctes.conStringAdoGeneral);
            try
            {
            sqlt.Append(" update ${table} set");
		<#assign count=0>		
		<#list table.getFields() as field>
		 	 <#-- segun el tipo de campo...   -->
			 <#switch field.getType()>
				  <#case "_int">
						sqlt.Append(" ${field}=" + sf.cadena(obj${table}.${field})+ "<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ") ;
					 <#break>
				  <#case "_string">
					sqlt.Append(" ${field}='" + sf.cadena(obj${table}.${field})+ "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> "); 
					 <#break>
				  <#case "_date">
					  sqlt.Append(" ${field}='" + sf.fechaIso(obj${table}.${field}) + "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> " );
					 <#break>
				  <#case "_boolean">
					 sqlt.Append(" ${field}=" + sf.Bool(obj${table}.${field}) + "<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ");
					 <#break>
				  <#default>
					sqlt.Append(" ${field}='" + sf.cadena(obj${table}.${field})+ "'<#if (table.getNumberOfFields() -  count  != 1)>,</#if> ") ;
			</#switch> 			 
		 <#assign count = count+1>
		</#list>
		          
		sqlt.Append(" where  ${table.getKey()}=" + sf.cadena(obj${table}.${table.getKey()}));        
		reg = db.sql(sqlt.ToString());
             return (reg!=null);
              }   
            catch (Exception ex)
            {
                // Argument is optional, no "When" keyword 
            }
            finally
            {
                db.Dispose();              
            }
             return true;
       }

	
 
}